<?php
namespace app\model;
use think\model;
use think\facade\Db;
class Article extends Model{

    protected $table = "user_article";


    // 直接插入
    public function insertArticle($data){
        $at = Article::Create([
            "id"=>$data["id"],
            "title"=>$data["title"],
            "summary"=>$data["summary"],
            "content"=>$data["content"],
            "addTime"=>$data["addTime"],
            "lastUpdTime"=>$data["lastUpdTime"],
            "image"=>$data["image"],
            "range"=>$data["range"],
            "atType"=>$data["atType"],
            "uid"=>$data["uid"],
            "labList"=>$data["labList"],
            "colList"=>$data["colList"],
        ]);
        return $at;
    }

//    public function saveArticle($data,$uid,$id){
//        foreach ($data as $key=>$value){
//            $article = new Article();
//            $article->updateArticle($id,$uid,$key,$value);
//        }
//
//    }

    public function saveArticle($data,$uid,$id){
        Article::update([
            "summary"=>$data["summary"][0],
            "range"=>$data["range"][0],
            "content"=>$data["content"][0],
            "lastUpdTime"=>$data["lastUpdTime"][0],
            "labList"=>$data["labList"][0],
            "colList"=>$data["colList"][0],
        ],[
            'id'=>"$id",
            'uid'=>"$uid",
        ]);

    }

    public function updateArticle($id,$uid,$key,$value){

        if ($value==""){
            Article::update([
                "$key"=>" "
            ],[
                'id'=>"$id",
                'uid'=>"$uid",
            ]);
        }else{
            Article::update([
                "$key"=>$value
            ],[
                'id'=>"$id",
                'uid'=>"$uid",
            ]);
        }

    }

    // 查询单个文章
    function queryArticleById($id,$uid){
        $article = Article::where('id',$id)->where('uid',$uid)->find();
        return $article->toArray();
    }

    // 查询当前用户的文章
    function queryArticleAllByUser($uid,$page,$size){
        $article = Article::where('uid',$uid)->limit($page,$size)->select();
        return $article->toArray();
    }


    // 统计数量
    function countAt($uid){
        $article = Article::where('uid',$uid)->count();
        return $article;
    }

    // 多条件模糊查询
    function queryLike($time,$label,$col,$title,$page,$size,$uid){
        $article = Article::where('addTime','like',"%$time%")
                                ->where('labList','like',"%$label%")
                                ->where('colList','like',"%$col%")
                                ->where('title','like',"%$title%")
                                ->limit($page,$size)
                                ->where('uid',$uid)
                                ->select();
        //dump($article->toArray());
        return $article->toArray();
    }

    // 统计数量2
    function countAtLike($time,$label,$col,$title,$page,$size,$uid){
        $article = Article::where('addTime','like',"%$time%")
            ->where('labList','like',"%$label%")
            ->where('colList','like',"%$col%")
            ->where('title','like',"%$title%")
            ->limit($page,$size)
            ->where('uid',$uid)
            ->count();
        return $article;
    }


    // 删除
    function delAtById($id){
        Db::table('user_article')->where('id',$id)->delete();
    }
}